package com.gdut.simple.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gdut.simple.R;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;

public class PhotoViewPagerAdapter extends PagerAdapter {

    private Context context;
    private List<String> images;
    private SparseArray<View> cacheView;
    private ViewGroup containerGroup;
    private LayoutInflater inflater;

    public PhotoViewPagerAdapter(Context context, List<String> images) {
        this.context = context;
        this.images = images;
        cacheView = new SparseArray<>(images.size());
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return images != null ? images.size() : 0;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (containerGroup == null) {
            containerGroup = container;
        }
        View view = cacheView.get(position);
        if (view == null) {
            view = inflater.inflate(R.layout.layout_vp_image_item, container, false);
            view.setTag(position);
            final ImageView image = (ImageView) view.findViewById(R.id.id_image);
            final PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(image);

            if (images.get(position).startsWith("http")) {
                Picasso.with(context)
                        .load(images.get(position))
                        .placeholder(R.drawable.ic_place_holder)
                        .error(R.drawable.ic_load_error)
                        .config(Bitmap.Config.RGB_565)
                        .into(image, new Callback() {
                            @Override
                            public void onSuccess() {
                                photoViewAttacher.update();
                            }

                            @Override
                            public void onError() {

                            }
                        });
            } else {
                Picasso.with(context)
                        .load(new File(images.get(position)))
                        .placeholder(R.drawable.ic_place_holder)
                        .error(R.drawable.ic_load_error)
                        .config(Bitmap.Config.RGB_565)
                        .into(image, new Callback() {
                            @Override
                            public void onSuccess() {
                                photoViewAttacher.update();
                            }

                            @Override
                            public void onError() {

                            }
                        });
            }


            photoViewAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float v, float v1) {
                    Activity activity = (Activity) context;
                    activity.finish();
                    activity.overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
                }

                @Override
                public void onOutsidePhotoTap() {

                }
            });
        }
        container.addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}
